
//
// 120327 - AH - (Midtun) Created.
// 120714 - AH - Took FOLDER_ID in use.
//

package org.soekrat.server.servlets.file;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.japril.servlet.HttpRequestDef;
import org.soekrat.server.io.db.Folder;
import org.soekrat.server.io.db.SoekratDAO;
import org.soekrat.server.io.db.SoekratDAOFactory;
import org.soekrat.server.serialization.FolderSerializable;
import org.soekrat.server.servlets.AbstractSoekratHttpServlet;

/**
 * <p>
 *   Implements the request to get a
 *   {@link Folder}.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
@SuppressWarnings("serial")
public final class GetFolderHttpServlet extends AbstractSoekratHttpServlet
{
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public GetFolderHttpServlet(){}
  
  @Override
  protected void handleRequest(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException 
  {
    HttpRequestDef def=getHttpRequestDef();
    
    if (!hasParameters(req))
    {
      PrintWriter pw=printSimpleHtmlStart(res);
      pw.println(def.compileApiHtml());
      printSimpleHtmlEnd(pw);
    }
    else
    {
      if (!validate(res, def, req)) return;
      
      long key=def.fetch(FOLDER_ID.name(), req).getLongValue();
      
      SoekratDAO dao=new SoekratDAOFactory().create();
          
      Folder folder=dao.getFolder(key);
      
      respond(res, new FolderSerializable(folder));
    }
  }
  
  @Override
  public HttpRequestDef getHttpRequestDef()
  {
    return new HttpRequestDef("/soekrat_server/get_folder").desc("Get Folder")
      .parameter(FOLDER_ID);
  }
  
}
